local EmoTalentUtil = require "packages.emopack.pkg.emopack_talent.util" -- 加载战法核心
local skel = require "packages.emopack.pkg.emopack_talent.talents.core" ---@type SkillSkeleton

local hasTalent = EmoTalentUtil.hasTalent
local logTalent = EmoTalentUtil.logTalent
local invokeTalent = EmoTalentUtil.invokeTalent
local removeTalent = EmoTalentUtil.removeTalent
local discoverTalent = EmoTalentUtil.discoverTalent
local forRandom = EmoTalentUtil.forRandom
local getTalents = EmoTalentUtil.getTalents
local gainTalent = EmoTalentUtil.gainTalent
local getDynValue = EmoTalentUtil.getDynValue
local addDynValue = EmoTalentUtil.addDynValue
local handleInvokedTalents = EmoTalentUtil.handleInvokedTalents
local timesCheckFilter = EmoTalentUtil.timesCheckFilter



--- 造成伤害时，修改伤害加成
skel:addEffect(fk.DamageCaused, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    local room = player.room
    local cost_data = {}
    if hasTalent(player, "emot_manwang") and not player.dead and data.card
     and data.card.trueName == "savage_assault" and player:isEnemy(data.to) then -- 蛮王
      table.insert(cost_data, "emot_manwang")
    end
    if target == player then -- 我造成伤害
      if hasTalent(player, "emot_shengsi") and data.card and data.card.trueName == "duel" and player:isEnemy(data.to) then -- 一决生死
        table.insert(cost_data, "emot_shengsi")
      end
      if hasTalent(player, "emot_shouchao") and #getTalents(player, "beast") >= 3 then -- 兽潮
        table.insert(cost_data, "emot_shouchao")
      end
      if hasTalent(player, "emot_jinbei") and EmoTalentUtil:getMoney(player) >= 50 then -- 黄金酒杯
        table.insert(cost_data, "emot_jinbei")
      end
      if hasTalent(player, "emot_youshang") and player:isFriend(data.to) then -- 友伤
        table.insert(cost_data, "emot_youshang")
      end
      if hasTalent(player, "emot_renzhe") and player:isEnemy(data.to) and data.card and data.card.type == Card.TypeTrick then -- 忍者
        table.insert(cost_data, "emot_renzhe")
      end
      if hasTalent(player, "emot_juji") and player:isEnemy(data.to) and not data.to:inMyAttackRange(player) then -- 狙击手
        table.insert(cost_data, "emot_juji")
      end
      if hasTalent(player, "emot_quanshi") and data.damage == 1 and data.damageType == fk.NormalDamage then -- 拳师
        table.insert(cost_data, "emot_quanshi")
      end
      if hasTalent(player, "emot_juxi") and player.shield > 1 and data.damageType == fk.FireDamage then -- 熔岩巨蜥
        table.insert(cost_data, "emot_juxi")
      end
      if hasTalent(player, "emot_relie") and player:getMark("emot_relie") == 0 then -- 热烈
        table.insert(cost_data, "emot_relie")
      end
      if data.card and data.card.trueName == "slash" then -- 我的【杀】的伤害
        if hasTalent(player, "emot_cigu") and table.every(room.alive_players, function (p) return p.hp >= data.to.hp end) then -- 刺骨
          table.insert(cost_data, "emot_cigu")
        end
        if player:getMark("emot_yinxue") > 0 then -- 饮血刀
          table.insert(cost_data, "emot_yinxue")
        end
        if hasTalent(player, "emot_tulong") and player:isEnemy(data.to) and data.to.maxHp > 4 then -- 屠龙宝刀
          table.insert(cost_data, "emot_tulong")
        end
      end
      if hasTalent(player, "emot_xiaohun") then -- 销魂
        local num = #room.logic:getEventsOfScope(GameEvent.Death, 99, function(e)
          return e.data.killer == player
        end, Player.HistoryRound)
        if num > 0 then
          cost_data["emot_xiaohun"] = num
          table.insert(cost_data, "emot_xiaohun")
        end
      end
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    if table.contains(cost_data, "emot_quanshi") then -- 拳师优先结算
      invokeTalent(player, "emot_quanshi", event)
      data:changeDamage(1)
    end
    if table.contains(cost_data, "emot_shengsi") then -- 一决生死
      local damage = data.to.shield + math.max(data.to.hp, 0)
      if damage > data.damage then
        invokeTalent(player, "emot_shengsi", event)
        data:changeDamage(damage - data.damage)
      end
    end
    if table.contains(cost_data, "emot_renzhe") then -- 忍者
      invokeTalent(player, "emot_renzhe", event)
      data:changeDamage(1)
    end
    if table.contains(cost_data, "emot_juji") then -- 狙击手
      invokeTalent(player, "emot_juji", event)
      data:changeDamage(1)
    end
    if table.contains(cost_data, "emot_relie") and player:getMark("emot_relie") == 0 then -- 热烈
      invokeTalent(player, "emot_relie", event)
      room:setPlayerMark(player, "emot_relie", 1)
      data:changeDamage(3)
    end
    if table.contains(cost_data, "emot_juxi") then -- 熔岩巨蜥
      invokeTalent(player, "emot_juxi", event, 2)
      data:changeDamage(player.shield // 2)
    end
    if table.contains(cost_data, "emot_tulong") then -- 屠龙宝刀
      invokeTalent(player, "emot_tulong", event)
      data:changeDamage(data.to.maxHp // 5)
    end
    if table.contains(cost_data, "emot_shouchao") then -- 兽潮
      invokeTalent(player, "emot_shouchao", event)
      local num = #getTalents(player, "beast") // 3
      data:changeDamage(num)
    end
    if table.contains(cost_data, "emot_jinbei") then -- 黄金酒杯
      invokeTalent(player, "emot_jinbei", event, 2)
      local num = EmoTalentUtil:getMoney(player) // 50
      data:changeDamage(num)
    end
    if table.contains(cost_data, "emot_xiaohun") then -- 销魂
      invokeTalent(player, "emot_xiaohun", event)
      local num = cost_data["emot_xiaohun"] or 1
      data:changeDamage(num)
    end
    if table.contains(cost_data, "emot_youshang") and data.damage > 0 then -- 防止友伤，尽量在减伤前结算
      invokeTalent(player, "emot_youshang", event)
      local num = data.damage
      data:preventDamage()
      if not data.to.dead then
        data.to:drawCards(num, skel.name)
      end
    end
    if table.contains(cost_data, "emot_manwang") and data.damage ~= 0 then -- 蛮王
      invokeTalent(player, "emot_manwang", event)
      data:changeDamage(6)
    end
    -- 刺骨翻倍伤害，需要在修改伤害后结算
    if table.contains(cost_data, "emot_cigu") then
      invokeTalent(player, "emot_cigu", event)
      data:changeDamage(data.damage)
    end
    if table.contains(cost_data, "emot_yinxue") then -- 饮血刀
      invokeTalent(player, "emot_yinxue", event)
      data:changeDamage(player:getMark("emot_yinxue"))
      room:setPlayerMark(player, "emot_yinxue", 0)
    end
    handleInvokedTalents(player, event)
  end,
})

--- 受到伤害确认时，比受到伤害时更晚，用于确认保底伤害
skel:addEffect(fk.DetermineDamageInflicted, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    local cost_data = {}
    if target == player then
      if hasTalent(player, "emot_molin") and #player:getCardIds("e") > 0 and data.damage > 1 then -- 墨鳞甲
        table.insert(cost_data, "emot_molin")
      end
      if hasTalent(player, "emot_shengdun") and player:getMark("emot_shengdun") > 0 then -- 圣盾
        table.insert(cost_data, "emot_shengdun")
      end
      if player:getMark("emot_chongcao") > 0 then -- 冬虫夏草
        table.insert(cost_data, "emot_chongcao")
      end
      if hasTalent(player, "emot_wuwo") and player:isKongcheng() then -- 无我
        table.insert(cost_data, "emot_wuwo")
      end
      if hasTalent(player, "emot_kuilei") and #player:getCardIds("e") > 0 then -- 傀儡
        table.insert(cost_data, "emot_kuilei")
      end
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    --- 防止伤害
    if table.contains(cost_data, "emot_wuwo") and data.damage > 0 then -- 无我
      invokeTalent(player, "emot_wuwo", event)
      data:preventDamage()
    end
    if table.contains(cost_data, "emot_shengdun") and data.damage > 0 then -- 圣盾
      invokeTalent(player, "emot_shengdun", event)
      room:setPlayerMark(player, "emot_shengdun", 0)
      data:preventDamage()
    end
    if table.contains(cost_data, "emot_chongcao") and data.damage > 0 then -- 冬虫夏草
      invokeTalent(player, "emot_chongcao", event)
      room:setPlayerMark(player, "emot_chongcao", 0)
      data:preventDamage()
    end
    if table.contains(cost_data, "emot_kuilei") and #player:getCardIds("e") > 0 and data.damage > 0 then
      invokeTalent(player, "emot_kuilei", event)
      data:preventDamage()
      local cid = table.random(player:getCardIds("e"))
      player:addToPile("emot_kuilei", cid, false, skel.name)
    end
    if table.contains(cost_data, "emot_molin") and #player:getCardIds("e") > 0 and data.damage > 1 then -- 墨鳞甲
      invokeTalent(player, "emot_molin", event)
      data:changeDamage(1 - data.damage)
    end
    handleInvokedTalents(player, event)
  end,
})

--- 受到伤害时
skel:addEffect(fk.DamageInflicted, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    local room = player.room
    local cost_data = {}
    if target == player then -- 我受到伤害
      if hasTalent(player, "emot_godshield") and ((not data.from) or data.from.phase ~= Player.Play) then -- 上帝之盾
        table.insert(cost_data, "emot_godshield")
      end
      if hasTalent(player, "emot_pangpang") and data.damageType == player:getMark("emot_pangpang") then -- 胖胖熊猫
        table.insert(cost_data, "emot_pangpangD") -- 需要和另一个时机的标记不一样
      end
      if hasTalent(player, "emot_qixiang") then -- 气象学
        if data.damageType ~= fk.NormalDamage or #getTalents(player, "weather") > 0 then
          table.insert(cost_data, "emot_qixiang")
        end
      end
      if player:getMark("emot_modun-turn") ~= 0 then -- 魔法护盾
        table.insert(cost_data, "emot_modun")
      end
      if hasTalent(player, "emot_yinghuo") and not data.card then -- 萤火虫巢穴
        table.insert(cost_data, "emot_yinghuo")
      end
      if hasTalent(player, "emot_huadi") and #player:getCardIds("j") > 0 then -- 画地为牢
        table.insert(cost_data, "emot_huadi")
      end
      if hasTalent(player, "emot_turang") then -- 厚实土壤
        table.insert(cost_data, "emot_turang")
      end
    else -- 其他角色受到伤害
      if hasTalent(player, "emot_tideheart") and player:isEnemy(target) and not player.dead
       and data.damageType == fk.IceDamage then -- 潮汐之心
        table.insert(cost_data, "emot_tideheart")
      end
      if hasTalent(player, "emot_yueshi") and player:isEnemy(target) and not player.dead then -- 月食
        local current = room.current
        if current and player:isEnemy(current) then
          table.insert(cost_data, "emot_yueshi")
        end
      end
    end
    if hasTalent(player, "emot_mushi") and player:isFriend(target) and data.card and data.card.trueName == "slash" then -- 牧师
      table.insert(cost_data, "emot_mushi")
    end
    if hasTalent(player, "emot_qiushan") and data.damageType == fk.ThunderDamage
    and player:isEnemy(target) and not player.dead then -- 球状闪电
      table.insert(cost_data, "emot_qiushan")
    end
    if player:getMark("emot_yanyin-turn") ~= 0 then -- 烟瘾
      table.insert(cost_data, "emot_yanyin")
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    if table.contains(cost_data, "emot_yanyin") then -- 烟瘾
      invokeTalent(player, "emot_yanyin", event, 1)
      data:changeDamage(1)
    end
    if table.contains(cost_data, "emot_yueshi") then -- 月食
      invokeTalent(player, "emot_yueshi", event)
      data:changeDamage(3)
    end
    if table.contains(cost_data, "emot_tideheart") then -- 潮汐之心
      invokeTalent(player, "emot_tideheart", event)
      data:changeDamage(3)
    end
    if table.contains(cost_data, "emot_pangpangD") and data.damage > 0 then -- 胖胖
      invokeTalent(player, "emot_pangpang", event)
      data:preventDamage()
    end
    if table.contains(cost_data, "emot_modun") and data.damage > 0 then -- 魔力护盾
      invokeTalent(player, "emot_modun", event, 1)
      data:preventDamage()
    end
    if table.contains(cost_data, "emot_mushi") and data.damage > 0 then -- 牧师
      invokeTalent(player, "emot_mushi", event)
      data:changeDamage(-1)
    end
    if table.contains(cost_data, "emot_qixiang") then -- 气象学
      invokeTalent(player, "emot_qixiang", event)
      local num = 0
      if data.damageType ~= fk.NormalDamage then
        num = 2
      end
      if #getTalents(player, "weather") > 0 then
        num = num + 1
      end
      data:changeDamage(-num)
    end
    if table.contains(cost_data, "emot_yinghuo") and data.damage > 0 then -- 萤火虫巢穴
      invokeTalent(player, "emot_yinghuo", event)
      data:preventDamage()
    end
    if table.contains(cost_data, "emot_turang") and data.damage > 0 then -- 厚实土壤
      invokeTalent(player, "emot_turang", event)
      local num = 0
      local cards = room:getNCards(3)
      room:moveCardTo(cards, Card.Processing, nil, fk.ReasonJustMove, skel.name, nil, true)
      for _, cid in ipairs(cards) do
        if Fk:getCardById(cid).type == Card.TypeBasic then
          num = num + 1
          room:setCardEmotion(cid, "judgegood")
        end
      end
      room:delay(600)
      room:cleanProcessingArea(cards)
      if num > 0 then
        data:changeDamage(-num)
      end
    end
    -- 需要消耗的防止伤害，延后结算
    if table.contains(cost_data, "emot_godshield") and data.damage > 0 then -- 上帝之盾
      invokeTalent(player, "emot_godshield", event)
      data:changeDamage(-3)
    end
    if table.contains(cost_data, "emot_huadi") and data.damage > 0 and #player:getCardIds("j") > 0 then -- 画地为牢
      invokeTalent(player, "emot_huadi", event, 1)
      data:preventDamage()
      local cid = room:askToChooseCard(player, { target = player, flag = "j", skill_name = "emot_huadi" })
      room:obtainCard(player, cid, true, fk.ReasonPrey, player, skel.name)
    end
    if table.contains(cost_data, "emot_qiushan") and forRandom(player, 0.2, "emot_qiushan") then -- 球状闪电
      invokeTalent(player, "emot_qiushan", event)
      local skills = target:getSkillNameList()
      if #skills > 0 then
        room:handleAddLoseSkills(target, "-" .. table.random(skills))
      end
      data:changeDamage(data.damage)
    end
    handleInvokedTalents(player, event)
  end,
})

-- 受到伤害后
skel:addEffect(fk.Damaged, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    local room = player.room
    local cost_data = {}
    if target == player then -- 我受伤害
      if not player.dead then
        -- 圣母怜爱，圣盾，魔脑，胖胖熊猫，适应元件，异端，龙蛋
        for _, t in ipairs({"emot_shengmu", "emot_shengdun", "emot_monao", "emot_pangpang", "emot_shiying", "emot_yiduan", "emot_longdan"}) do
          if hasTalent(player, t) then
            table.insert(cost_data, t)
          end
        end
      end
      if hasTalent(player, "emot_niliu") and data.damage > 1 and not player.dead then -- 逆流之愈
        table.insert(cost_data, "emot_niliu")
      end
      if hasTalent(player, "emot_jiting") then -- 急停
        local current = room.current
        if current and player:isEnemy(current) and table.find(player.player_skills, function(skill)
          return skill.visible and not skill.name:endsWith("&") and not skill:isEffectable(player)
        end) then
          table.insert(cost_data, "emot_jiting")
        end
      end
      if data.from and player:isEnemy(data.from) and not data.from.dead then -- 受到敌人伤害且其存活
        if hasTalent(player, "emot_tiaobo") and
         table.find(room.alive_players, function(p) return p ~= data.from and player:isEnemy(p) end) then -- 挑拨
          table.insert(cost_data, "emot_tiaobo")
        end
        if hasTalent(player, "emot_jingji") then -- 荆棘
          table.insert(cost_data, "emot_jingji")
        end
        if hasTalent(player, "emot_yingxie") then -- 影蝎
          table.insert(cost_data, "emot_yingxie")
        end
      end
      if hasTalent(player, "emot_tuotu") and data.card and data.card.trueName == "slash" then -- 脱兔
        table.insert(cost_data, "emot_tuotu")
      end
    end
    if hasTalent(player, "emot_bingbao") and player:isEnemy(target) and data.damageType == fk.NormalDamage then -- 冰雹
      table.insert(cost_data, "emot_bingbao")
    end
    if hasTalent(player, "emot_nishiliu") and player:isEnemy(target)
     and data.damageType == fk.NormalDamage and not target:isNude() then -- 泥石流
      table.insert(cost_data, "emot_nishiliu")
    end
    if hasTalent(player, "emot_fenfeng") and player:isEnemy(target) and data.damageType == fk.FireDamage -- 焚风
     and table.find(room.alive_players, function (p) return p ~= data.to and player:isEnemy(p) end) then
      table.insert(cost_data, "emot_fenfeng")
    end
    if hasTalent(player, "emot_fanchao") and player:isEnemy(target) and data.chain then -- 返潮
      table.insert(cost_data, "emot_fanchao")
    end
    if hasTalent(player, "emot_canshi") and player:isEnemy(target) and not target.dead and target.hp > 0 then -- 蚕食
      table.insert(cost_data, "emot_canshi")
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    if table.contains(cost_data, "emot_shengdun") then -- 圣盾
      invokeTalent(player, "emot_shengdun", event)
      room:setPlayerMark(player, "emot_shengdun", 1)
    end
    if table.contains(cost_data, "emot_pangpang") then -- 胖胖
      invokeTalent(player, "emot_pangpang", event)
      room:setPlayerMark(player, "emot_pangpang", data.damageType)
    end
    if table.contains(cost_data, "emot_yiduan") then -- 异端
      local targets = table.filter(room.alive_players, function (p) return player:isEnemy(p) and p:getMark("emot_invalid-turn") == 0 end)
      if #targets > 0 then
        invokeTalent(player, "emot_yiduan", event)
        local to
        if data.from and table.contains(targets, data.from) then -- 优先封印伤害来源
          to = data.from
        else
          to = table.random(targets)
        end
        room:doIndicate(player, {to})
        room:setPlayerMark(to, "emot_invalid-turn", 1)
      end
    end
    if table.contains(cost_data, "emot_jiting") then -- 急停
      invokeTalent(player, "emot_jiting", event)
      room:endTurn()
    end
    --- 实际操作
    if table.contains(cost_data, "emot_monao") and not player.dead then -- 魔脑
      invokeTalent(player, "emot_monao", event)
      local choices = {}
      local tchoices = EmoTalentUtil:getAvailableTalents(player) -- 可选战法
      if #tchoices > 0 then
        table.insert(choices, table.random(tchoices))
      end
      local skills = {} -- 可选技能
      for _, g in ipairs(Fk:getAllGenerals()) do
        for _, s in ipairs(g:getSkillNameList(true)) do
          if not player:hasSkill(s, true, true) then
            table.insertIfNeed(skills, s)
          end
        end
      end
      if #skills > 0 then
        table.insert(choices, table.random(skills))
      end
      table.insert(choices, "draw3")
      local choice = room:askToChoice(player, { choices = choices, detailed = true, skill_name = "emot_monao", prompt = " " })
      if choice == "draw3" then
        player:drawCards(3, skel.name)
      elseif choice:startsWith("emot") then
        gainTalent(player, choice)
      else
        room:handleAddLoseSkills(player, choice)
      end
    end
    if table.contains(cost_data, "emot_longdan") and not player.dead then -- 龙蛋
      room:addPlayerMark(player, "emot_longdan")
      if player:getMark("emot_longdan") > 3 then
        room:setPlayerMark(player, "emot_longdan", 0)
        invokeTalent(player, "emot_longdan", event)
        room:changeShield(player, 4)
        removeTalent(player, "emot_longdan")
        discoverTalent(player, { style = "beast", num = 8 })
      end
    end
    if table.contains(cost_data, "emot_shengmu") and not player.dead then -- 圣母怜爱
      invokeTalent(player, "emot_shengmu", event)
      room:changeMaxHp(player, 1)
      player:reset()
      local sealed = table.simpleClone(player.sealedSlots)
      table.removeOne(sealed, Player.JudgeSlot)
      if #sealed > 0 then
        room:resumePlayerArea(player, sealed)
      end
    end
    if table.contains(cost_data, "emot_canshi") and not target.dead and target.hp > 0 then -- 蚕食
      invokeTalent(player, "emot_canshi", event)
      room:loseHp(target, data.damage, skel.name)
    end
    if table.contains(cost_data, "emot_tiaobo") and data.from and not data.from.dead then -- 挑拨
      for _, p in ipairs(room.alive_players) do
        if p ~= data.from and player:isEnemy(p) and not p.dead then
          local slash = table.find(p:getCardIds("h"), function (id)
            return Fk:getCardById(id).trueName == "slash" and not p:isProhibited(data.from, Fk:getCardById(id))
          end)
          if slash then
            invokeTalent(player, "emot_tiaobo", event)
            room:useCard{
              from = p, tos = {data.from}, card = Fk:getCardById(slash), extraUse = true,
            }
            break
          end
        end
      end
    end
    if table.contains(cost_data, "emot_niliu") and not player.dead then -- 逆流之愈
      invokeTalent(player, "emot_niliu", event)
      room:recover { num = data.damage // 2, skillName = skel.name, who = player, recoverBy = player }
    end
    if table.contains(cost_data, "emot_bingbao") and forRandom(player, 0.2, "emot_bingbao") then -- 冰雹
      invokeTalent(player, "emot_bingbao", event)
      room:delay(250)
      room:damage { damage = data.damage, from = data.from, skillName = skel.name, to = data.to, damageType = fk.IceDamage }
    end
    if table.contains(cost_data, "emot_fanchao") and not target.dead then -- 返潮
      invokeTalent(player, "emot_fanchao", event)
      room:loseHp(target, data.damage, skel.name)
    end
    if table.contains(cost_data, "emot_nishiliu") and not target:isNude() then -- 泥石流
      invokeTalent(player, "emot_nishiliu", event)
      if forRandom(player, 0.2, "emot_nishiliu") then
        target:throwAllCards("he", skel.name)
      else
        room:askToDiscard(target, {
          min_num = data.damage, max_num = data.damage, include_equip = true, skill_name = skel.name, cancelable = false
        })
      end
    end
    if table.contains(cost_data, "emot_fenfeng") and forRandom(player, 0.2, "emot_fenfeng") then -- 焚风
      local targets = table.filter(room.alive_players, function (p) return p ~= data.to and player:isEnemy(p) end)
      if #targets > 0 then
        invokeTalent(player, "emot_fenfeng", event)
        local to = table.random(targets)
        room:doIndicate(target, {to})
        room:delay(100)
        room:damage { damage = data.damage+1, from = data.from, skillName = skel.name, to = to, damageType = fk.FireDamage }
      end
    end
    if table.contains(cost_data, "emot_shiying") and not player.dead then -- 适应元件
      local ids = table.filter(player:getCardIds("he"), function(id) return not player:prohibitDiscard(id) end)
      if #ids > 0 then
        invokeTalent(player, "emot_shiying", event)
        room:throwCard(table.random(ids), skel.name, player, player)
        room:changeShield(player, 1)
      end
    end
    if table.contains(cost_data, "emot_tuotu") and not player.dead then -- 脱兔
      local ids = room:getCardsFromPileByRule(".|.|heart,diamond|.|.|basic", 3, "allPiles")
      if #ids > 0 then
        invokeTalent(player, "emot_tuotu", event)
        room:obtainCard(player, ids, true, fk.ReasonJustMove, player, skel.name)
      end
    end
    if table.contains(cost_data, "emot_jingji") and not data.from.dead then -- 荆棘
      invokeTalent(player, "emot_jingji", event)
      room:delay(250)
      room:doIndicate(player, {data.from})
      room:damage { damage = data.damage, from = player, skillName = skel.name, to = data.from }
    end
    if table.contains(cost_data, "emot_yingxie") and not data.from.dead then -- 影蝎
      invokeTalent(player, "emot_yingxie", event)
      local num = getDynValue(player, "emot_yingxie")
      room:loseHp(data.from, num, skel.name)
      if not data.from.dead then
        addDynValue(player, "emot_yingxie", 1)
      end
    end
    handleInvokedTalents(player, event)
  end,
})

-- 造成伤害后
skel:addEffect(fk.Damage, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    local cost_data = {}
    local room = player.room
    if target == player then -- 我造成伤害后
      if data.card and data.card.trueName == "slash" then -- 【杀】造成伤害
        if hasTalent(player, "emot_heianjian") and player:isEnemy(data.to) and data.to.maxHp > 0 then -- 黑暗剑
          table.insert(cost_data, "emot_heianjian")
        end
        if hasTalent(player, "emot_hound") and not player.dead then -- 猎犬
          table.insert(cost_data, "emot_hound")
        end
        if hasTalent(player, "emot_feidao") and table.find(room.alive_players, function (p)
           return p ~= data.to and p:isEnemy(player) end) then -- 飞刀
          table.insert(cost_data, "emot_feidao")
        end
          if hasTalent(player, "emot_chuanxin") and data.damage > 1 and player:isEnemy(data.to)
          and data.to.faceup and not data.to.dead then -- 穿心
          table.insert(cost_data, "emot_chuanxin")
        end
      end
      if hasTalent(player, "emot_juxi") and data.damageType == fk.FireDamage then -- 熔岩巨蜥
        table.insert(cost_data, "emot_juxi")
      end
      if hasTalent(player, "emot_huoliu") and data.damageType == fk.FireDamage and player:isEnemy(data.to) and not data.to.dead then -- 火瘤
        table.insert(cost_data, "emot_huoliu")
      end
      if hasTalent(player, "emot_huangzai") and player:isEnemy(data.to) and not data.to.dead then -- 蝗虫过境
        table.insert(cost_data, "emot_huangzai")
      end
    else -- 其他角色造成伤害
      if not player.dead and target and not target.dead and player:isEnemy(target) then
        if hasTalent(player, "emot_zhuzhou") then -- 助纣为虐
          table.insert(cost_data, "emot_zhuzhou")
        end
        if hasTalent(player, "emot_zize") and target:isFriend(data.to) then -- 自责
          table.insert(cost_data, "emot_zize")
        end
      end
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    if table.contains(cost_data, "emot_heianjian") and data.to.maxHp > 0 then -- 黑暗剑
      invokeTalent(player, "emot_heianjian", event)
      local num = math.min(data.damage, data.to.maxHp)
      room:changeMaxHp(data.to, -num)
      room:changeMaxHp(player, num)
    end
    if table.contains(cost_data, "emot_huangzai") and not data.to.dead then -- 蝗虫过境
      invokeTalent(player, "emot_huangzai", event)
      room:addPlayerMark(data.to, MarkEnum.MinusMaxCards)
      if data.to:getMaxCards() == 0 then
        data.to:throwAllCards("h", skel.name)
      end
    end
    if table.contains(cost_data, "emot_huoliu") and not data.to.dead then -- 火瘤
      invokeTalent(player, "emot_huoliu", event)
      local num = getDynValue(player, "emot_huoliu")
      addDynValue(player, "emot_huoliu", 2)
      room:loseHp(data.to, num, skel.name)
    end
    if table.contains(cost_data, "emot_zhuzhou") and not player.dead and not target.dead then -- 助纣为虐
      invokeTalent(player, "emot_zhuzhou", event)
      player:drawCards(3, skel.name)
      room:addPlayerMark(target, MarkEnum.SlashResidue .. "-phase", 1)
    end
    if table.contains(cost_data, "emot_juxi") and not player.dead then -- 熔岩巨蜥
      invokeTalent(player, "emot_juxi", event, 1)
      room:changeShield(player, 2)
    end
    if table.contains(cost_data, "emot_chuanxin") and data.to.faceup and not data.to.dead then -- 穿心
      invokeTalent(player, "emot_chuanxin", event)
      data.to:turnOver()
    end
    if table.contains(cost_data, "emot_hound") and not player.dead then -- 猎犬
      invokeTalent(player, "emot_hound", event)
      player:drawCards(1, skel.name)
      local cards = Card:getIdList(data.card)
      if #cards > 0 and not player.dead then
        local mark = player:getTableMark("emot_hound")
        table.insertTableIfNeed(mark, cards)
        room:setPlayerMark(player, "emot_hound", mark)
      end
    end
    if table.contains(cost_data, "emot_feidao") then -- 飞刀
      local targets = table.filter(room.alive_players, function (p)
        return p ~= data.to and p:isEnemy(player)
      end)
      if #targets > 0 then
        if forRandom(player, 0.2, "emot_feidao") then
          invokeTalent(player, "emot_feidao", event)
          local to = table.random(targets)
          room:doIndicate(player, {to})
          room:delay(150)
          room:damage { damage = 2 * data.damage, from = player, skillName = skel.name, to = to }
        end
      end
    end
    if table.contains(cost_data, "emot_zize") and not target.dead then -- 自责
      invokeTalent(player, "emot_zize", event)
      room:loseHp(target, data.damage, skel.name)
    end
    handleInvokedTalents(player, event)
  end,
})

-- 造成伤害前，用于修改伤害属性、伤害来源。时机非常早
skel:addEffect(fk.PreDamage, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    local cost_data = {}
    local room = player.room
    if hasTalent(player, "emot_qtpl") and not player.dead and player:isEnemy(data.to)
    and not data.to:isWounded() and data.damageType == fk.NormalDamage then -- 晴天霹雳
      table.insert(cost_data, "emot_qtpl")
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    if table.contains(cost_data, "emot_qtpl") then -- 晴天霹雳
      invokeTalent(player, "emot_qtpl", event)
      data.damageType = fk.ThunderDamage
      data:changeDamage(2)
    end
    handleInvokedTalents(player, event)
  end,
})

-- 伤害结算后
skel:addEffect(fk.DamageFinished, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    local cost_data = {}
    local room = player.room
    if hasTalent(player, "emot_jincha") and data.card and data.card.trueName == "slash"
    and not data.to.dead and data.damage <= 0 then -- 黄泉金叉
      table.insert(cost_data, "emot_jincha")
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    if table.contains(cost_data, "emot_jincha") and not data.to.dead then -- 黄泉金叉
      invokeTalent(player, "emot_jincha", event)
      room:loseHp(data.to, 2, skel.name)
    end
    handleInvokedTalents(player, event)
  end,
})

